package com.sxbbc.common.core.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.RefundVo;
import com.sxbbc.common.core.entity.jz.OrderDetail;
import com.sxbbc.common.core.entity.jz.ProSkuDetail;
import com.sxbbc.common.core.mapper.jz.OrderDetailMapper;
import com.sxbbc.common.core.service.IOrderDetailService;
import com.sxbbc.common.core.service.IOrderRefundLogService;
import com.sxbbc.common.core.service.IProSkuDetailService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoky
 * @since 2019-05-10
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService {
	@Autowired
	private IProSkuDetailService proSkuDetailService;
	@Autowired
	private IOrderRefundLogService orderRefundLogService;
	
	@Override
	public List<OrderDetail> listByOrderCode(String orderCode) {
		QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>();
		detailWrapper.eq("order_code", orderCode);
		List<OrderDetail> details = super.list(detailWrapper);
		for (OrderDetail detail : details) {
			ProSkuDetail skuDetail = proSkuDetailService.getById(detail.getProDetailId());
			if (skuDetail != null) {
				detail.setSkuId(skuDetail.getId());
				detail.setSeckillId(skuDetail.getSeckillId());
			}
			// 如果存在退款或退款成功
			if (CalcuUtils.getInstance().isEquals(detail.getRefund(), StaticUtils.STATUS_YES)) {
				// 查询对应的退款状态
				RefundVo refundDetail = orderRefundLogService.getRefundDetail(detail.getRefundCode());
				if (refundDetail != null) {
					detail.setRefundStatus(refundDetail.getStatus());
				}
			}
		}
		return details;
	}
	
}
